<?php
/**
 * projectname:gdu_cms2 
 * author:senman
 * time:2017/8/9 13:13
 */

namespace app\admin\controller;

use app\admin\builder\AdminConfigBuilder;
use app\admin\builder\AdminListBuilder;
use think\Request;

class Group extends Auth
{
    protected $user_model;
    protected $group_model;
    protected $access_model;
    protected $user_id;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->auth();
        $this->group_model = model('Group');
        $this->user_model = model('User');
        $this->access_model = model('Access');
        $this->user_id = session('user_id');
    }

    /**
     * @describe 组列表
     * @return mixed
     */
    public function groupList()
    {
        $data = $this->group_model->select();
        if ($data) {
            foreach ($data as $k => $v) {
                if ($v['status'] == 0) {
                    $data[$k]['status'] = 1;
                } else {
                    $data[$k]['status'] = 0;
                }
            }
        }
        $builderlist = new AdminListBuilder();
        $builderlist->title('权限管理')->suggest('角色列表');
        $builderlist->keyText('id', 'id');
        $builderlist->keyText('name', '账号');
        $builderlist->keyText('remark', '说明');
        $builderlist->keyText('create_time', '创建时间');
        $builderlist->keyText('update_time', '更新时间');
        if (checkAuth('admin/group/addHtml')) {
            $builderlist->buttonNew('添加', '', url('admin/group/addHtml'));
        }

        if (checkAuth('admin/group/addHtml')) {
            $builderlist->keyDoActionLink(url('admin/auth/access'), '编辑', 'btn-primary');
        }
        if (checkAuth('admin/group/changeState')) {
            $builderlist->keyDoActionPostTextChange(url('admin/group/changeState'), '启用', '禁用', 'status');

        }
        $builderlist->data($data);
        //写日志
        Log::writeLog("查看角色列表");
        return $builderlist->displayList();
    }


    public function addHtml()
    {
        $builder = new AdminConfigBuilder();
        $builder->formsubmit('新增角色');
        $builder->keyText('name', '角色名称');
        $builder->keyText('remark', '备注说明');
        $builder->keyRadio('status', '状态', '', ["0" => "启用", "1" => "禁用"]);
        $builder->buttonBack()->buttonSubmit(url('admin/group/add'));
        //写日志
        Log::writeLog("进入新增用户页面");
        return $builder->displayConfig();
    }

    public function add()
    {
        $validate = validate('Group');
        if (!$validate->scene('add')->check(input())) {
            return json(['status' => 0, 'info' => lang($validate->getError())]);
        }
        $data = [];
        $data['name'] = input('name');
        $data['status'] = input('status');
        $data['rules'] = '';
        $data['remark'] = input('remark');
        $data['create_time'] = time();
        $data['update_time'] = time();
        if ($this->group_model->insertGetId($data)) {
            //写日志
            Log::writeLog("新增角色");
            return json(['status' => 1, 'info' => lang('OPERATE_SUCCESS'), 'url' => url('admin/group/groupList')]);
        } else {
            return json(['status' => 0, 'info' => lang('OPERATE_FAILE')]);
        }
    }


    /**
     * 修改状态
     */
    public function changeState()
    {
        $input = $this->request->post();
        $id = $input['id'];
        if ($input['publish'] == "publish") {
            $status = 0;//启用
        } else {
            $status = 1;//禁用
        }
        $condition = [];
        $condition['id'] = $id;
        $query = $this->group_model->where($condition)->update(['status' => $status]);

        //写日志
        Log::writeLog("修改角色状态");
        if ($query) {
            return json(['status' => 1, 'info' => lang('OPERATE_SUCCESS'), 'url' => url('admin/group/groupList')]);
        } else {
            return json(['status' => 0, 'info' => lang('OPERATE_FAILE')]);
        }
    }

}